package com.example.a28693.duanshipin.presenters;

import android.nfc.Tag;
import android.support.annotation.Nullable;

import com.example.a28693.duanshipin.interfaces.IAlbumDetailPresenter;
import com.example.a28693.duanshipin.interfaces.IAlbumDetailViewCallBack;
import com.example.a28693.duanshipin.utils.Constants;
import com.example.a28693.duanshipin.utils.LogUtil;
import com.ximalaya.ting.android.opensdk.constants.DTransferConstants;
import com.ximalaya.ting.android.opensdk.datatrasfer.CommonRequest;
import com.ximalaya.ting.android.opensdk.datatrasfer.IDataCallBack;
import com.ximalaya.ting.android.opensdk.model.album.Album;
import com.ximalaya.ting.android.opensdk.model.track.Track;
import com.ximalaya.ting.android.opensdk.model.track.TrackList;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class AlbumDetailPresenterimpl implements IAlbumDetailPresenter {

    private static final String TAG                      = "AlbumDetailPresenterimpl" ;
    private List<IAlbumDetailViewCallBack> mCallBacks = new ArrayList<>();

    private Album mTargetAlbum = null;

    //单例
    private AlbumDetailPresenterimpl(){
    }

    private static AlbumDetailPresenterimpl sInstance = null;

   public static AlbumDetailPresenterimpl getInstance(){
        if (sInstance == null) {
            synchronized (AlbumDetailPresenterimpl.class){
                if (sInstance == null) {
                    sInstance = new AlbumDetailPresenterimpl();
                }
            }
        }
        return sInstance;
    }

    @Override
    public void pull2RefreshMore() {

    }

    @Override
    public void loadMore() {

    }

    @Override
    public void getAlbumDetail(int albumId, int page) {
       //根据页码和album专辑id获取列表
        Map<String, String> map = new HashMap<>();
        map.put(DTransferConstants.ALBUM_ID, albumId + "");
        map.put(DTransferConstants.SORT, "asc");
        map.put(DTransferConstants.PAGE, page + "");
        map.put(DTransferConstants.PAGE_SIZE, Constants.COUNT_DEFAULT + "");
        CommonRequest.getTracks(map, new IDataCallBack<TrackList>() {
            @Override
            public void onSuccess(@Nullable TrackList trackList) {
                LogUtil.d(TAG,"current thread -- >" + Thread.currentThread().getName());
                if (trackList != null) {
                    List<Track> tracks = trackList.getTracks();
                    LogUtil.d(TAG,"tracks size -- >" + tracks.size());
                    handlerAlbumDetailResult(tracks);
                }
            }

            @Override
            public void onError(int errorCode, String errorMsg) {
                LogUtil.d(TAG, "errorCode -- >" + errorCode);
                LogUtil.d(TAG, "errorMsg -- >" + errorMsg);
                handlerRrror(errorCode,errorMsg);
            }
        });
    }

    /**
     * 如果发生错误，就通知UI
     * @param errorCode
     * @param errorMsg
     */
    private void handlerRrror(int errorCode, String errorMsg) {
        for (IAlbumDetailViewCallBack callBack : mCallBacks) {
            callBack.onNetworkError(errorCode,errorMsg);
        }
    }

    private void handlerAlbumDetailResult(List<Track> tracks) {
        for (IAlbumDetailViewCallBack mCallBack : mCallBacks) {
            mCallBack.onDetailListLoaded(tracks);
        }
    }

    @Override
    public void registerViewCallBack(IAlbumDetailViewCallBack detailViewCallBack) {
        if (!mCallBacks.contains(detailViewCallBack)) {
            mCallBacks.add(detailViewCallBack);
            if (mTargetAlbum != null) {
                detailViewCallBack.onAlbumLoaded(mTargetAlbum);
            }
        }
    }

    @Override
    public void unRegisterViewCallBack(IAlbumDetailViewCallBack dtailViewCallBack) {
       mCallBacks.remove(dtailViewCallBack);
    }

    public void setTargetAlbum(Album targetAlbum){
        this.mTargetAlbum = targetAlbum;
    }
}
